/**
* get the transaction type
* if recv-qty
* YES: Establish the communication of parent and children
* NO : Do nothing
*
*/
function getTypeTx0(){
  
  var _COMMA_SEPARATOR  = ",";
  var _SMALLER          = 200;
  var _BASE_HTML        = "http://localhost/ecs/tocmenu_42.php";
  var _TYPE_PO          = 1;
  

  /**
  * Fetch the opener properties
  *
  */
  var _HEIGHT = 800 + _COMMA_SEPARATOR;
  var _WIDTH  = 600 + _COMMA_SEPARATOR;
  
  hdlChild = window.open((_BASE_HTML + "?transVariable=1"),
                         "quickChild",
                         ("height=" + _HEIGHT + 
                          "width="  + _WIDTH  + 
                          "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizeable=0"));
}

/**
* set serial number based on srcObject number and dstObject
*
* @param  String  srcObject
* @param  String  dstObject
*
*/
function setSerNumber2(srcObject, dstObject){
  
  var _RANGE_SYNC   = 1;
  var _DEFAULT_TEXT = "NULL";
  
  /**
  * Extract the source total qty
  *
  */
  srcObject = document.getElementsByName(srcObject);
  
  if(srcObject.length > 0){
    
    srcObject = srcObject[0];
  } 

  var srcValue = srcObject.value.split("-");
  
  if(!isNumeric(srcValue)){
    
    srcObject.value = 0;
  }
  
  /**
  * check if dstObject already has value
  *
  */
  dstObject = document.getElementsByName(dstObject);
  
  if(dstObject.length > 0){
    
    dstObject = dstObject[0];
  }
  
  if(dstObject.value.length > 0){
    
    var dstValue = dstObject.value.split("-");
    
    /**
    * Calc the range
    *
    */
    if(srcObject.value > 0){
      
      var tmpResult = parseFloat(dstValue)         + 
                      parseFloat(srcObject.value)  -
                      _RANGE_SYNC;
    }else{
      
      var tmpResult = parseFloat(dstValue);
    }
    
    /**
    * Concat the result
    *
    */
    dstObject.value = dstObject.value + " - " + tmpResult;
  }else{
    
    dstObject.value = _DEFAULT_TEXT;
  }
}

/**
* set the discount in amount
*
* @param  String  srcObject
* @param  String  thisObject
* @param  String  dstObject
*
*/
function setDiscount(srcObject,
                     thisObject,
                     dstObject){
  
  var _100_PCTG = 100;
  
  srcObject   = document.getElementsByName(srcObject);
  thisObject  = document.getElementsByName(thisObject);
  dstObject   = document.getElementsByName(dstObject);
  
  if(srcObject.length > 0){
    
    srcObject = srcObject[0];
  }
  
  if(thisObject.length > 0){
    
    thisObject = thisObject[0];
  }
  
  if(dstObject.length > 0){
    
    dstObject = dstObject[0];
  }
  
  /**
  * calc the amount of discount
  * result = srcAmount * (dscPctg / 100Pctg)
  *
  */
  var tmpResult = parseFloat(srcObject.value)   * 
                  (parseFloat(thisObject.value) / _100_PCTG);
                  
  dstObject.value = tmpResult.toFixed(2);
}


/**
* set the netto 
* netto = amount - discount
*
* @param  String  srcObject
* @param  String  thisObject
* @param  String  dstObject
*
*/
function setNetto(srcObject,
                  thisObject,
                  dstObject){
  
  srcObject   = document.getElementsByName(srcObject);
  thisObject  = document.getElementsByName(thisObject);
  dstObject   = document.getElementsByName(dstObject);
  
  if(srcObject.length > 0){
    
    srcObject = srcObject[0];
  }
  
  if(thisObject.length > 0){
    
    thisObject = thisObject[0];
  }
  
  if(dstObject.length > 0){
    
    dstObject = dstObject[0];
  }
  
  /**
  * Calc the netto
  *
  */
  var tmpResult = parseFloat(srcObject.value) - parseFloat(thisObject.value);
  
  dstObject.value = tmpResult.toFixed(2);
}

/**
* Calc pctg to amount
*
* @param  String  srcObject
* @param  String  thisObject
* @param  String  dstObject
*
*/
function setXPctg(srcObject,
                  thisObject,
                  dstObject){
  
  var _100_PCTG = 100;
  
  srcObject   = document.getElementsByName(srcObject);
  thisObject  = document.getElementsByName(thisObject);
  dstObject   = document.getElementsByName(dstObject);
  
  if(srcObject.length > 0){
    
    srcObject = srcObject[0];
  }
  
  if(thisObject.length > 0){
    
    thisObject = thisObject[0];
  }
  
  if(dstObject.length > 0){
    
    dstObject = dstObject[0];
  }
  
  /**
  * Calc tax amount
  *
  */
  var tmpResult   = parseFloat(srcObject.value)   * 
                    (parseFloat(thisObject.value) / _100_PCTG);
                  
  dstObject.value = tmpResult.toFixed(2);
}


/**
* Calc the payable
* Payable = amount + tax - dicount + expense - prepaid
*
* @param  string  amount
* @param  string  tax
* @param  string  discount
* @param  string  expense
* @param  string  prepaid
* @param  string  payable
*
*/
function calcPayable(amount, 
                     tax,
                     discount,
                     expense,
                     prepaid,
                     payable){
  
  amount    = document.getElementsByName(amount);
  tax       = document.getElementsByName(tax);
  discount  = document.getElementsByName(discount);
  expense   = document.getElementsByName(expense);
  prepaid   = document.getElementsByName(prepaid);
  payable   = document.getElementsByName(payable);
  
  if(amount.length > 0){
    
    amount = amount[0];
  }
  
  if(tax.length > 0){
    
    tax = tax[0];
  }
  
  if(discount.length > 0){
    
    discount = discount[0];
  }
  
  if(expense.length > 0){
    
    expense = expense[0];
  }
  
  if(prepaid.length > 0){
    
    prepaid = prepaid[0];
  }
  
  if(payable.length > 0){
    
    payable = payable[0];
  }
  
  /**
  * Calc
  *
  */
  var tmpResult = parseFloat(amount.value)  + parseFloat(tax.value) - parseFloat(discount.value) + 
                  parseFloat(expense.value) - parseFloat(prepaid.value);
                  
  payable.value = tmpResult.toFixed(2);
}

/**
* Get the mini Input
* 
* @param  string  srcHostIp
* @param  string  srcVarName
* @param  string  dstVarName
* @param  string  srcJid
* @param  string  srcQuantity
* @param  string  srcDtlNo
* @param  string  srcSubNo
*
*/
function getMiniInput(srcHostIp,
                      srcVarName, 
                      dstVarName, 
                      srcJid,
                      srcQuantity, 
                      srcDtlNo, 
                      srcSubNo){
  
  var _COMMA_SEPARATOR  = ",";
  var _SMALLER          = 200;
  var _BASE_HTML        = "http://" + srcHostIp + "/ecs/tocmenu_43/views/miniInput_html.php";
  var _TYPE_PO          = 1;
  
  /**
  * Fetch the opener properties
  *
  */
  var _HEIGHT = 250 + _COMMA_SEPARATOR;
  var _WIDTH  = 220 + _COMMA_SEPARATOR;
  
  hdlChild = window.open((_BASE_HTML + 
                          "?varName="     + srcVarName  +
                          "&dstVarName="  + dstVarName  + 
                          "&srcJid="      + srcJid      +
                          "&srcQuantity=" + srcQuantity +
                          "&DTL_NO="      + srcDtlNo    + 
                          "&SUB_NO="      + srcSubNo),
                         
                         "miniInput",
                         ("height=" + _HEIGHT + 
                          "width="  + _WIDTH  + 
                          "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizeable=0"));

}

/**
* Get the mini View
* 
* @param  string  srcHostIp
* @param  string  srcVarName
* @param  string  dstVarName
* @param  string  srcJid
* @param  string  srcDtlNo
* @param  string  srcSubNo
*
*/
function getMiniView(srcHostIp,
                     srcVarName, 
                     dstVarName, 
                     srcJid, 
                     srcDtlNo, 
                     srcSubNo){
  
  
  var _SEPARATOR_2ND    = "~^~";
  var _1ST              = 0;
  var _2ND              = 1;
  var _COMMA_SEPARATOR  = ",";
  var _SMALLER          = 200;
  var _BASE_HTML        = "http://" + srcHostIp + "/ecs/tocmenu_43/views/miniView_html.php";
  var _TYPE_PO          = 1;
  
  /**
  * Fetch the opener properties
  *
  */
  var _HEIGHT = 250 + _COMMA_SEPARATOR;
  var _WIDTH  = 220 + _COMMA_SEPARATOR;
  
  var srcSerial = document.getElementsByName(dstVarName);
  
  if(srcSerial.length > 0){
    
    srcSerial = srcSerial[0];
  }
  
  var serNumber = srcSerial.value.split(_SEPARATOR_2ND);
  
  hdlChild = window.open((_BASE_HTML + 
                          "?varName="     + srcVarName      +
                          "&dstVarName="  + dstVarName      + 
                          "&srcJid="      + serNumber[_2ND] +
                          "&DTL_NO="      + srcDtlNo        + 
                          "&SUB_NO="      + srcSubNo),
                         
                         "miniView",
                         ("height=" + _HEIGHT + 
                          "width="  + _WIDTH  + 
                          "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizeable=0"));

}

/**
* This is a test Function
* to test if the include is success
*
* @param  void
*
*/
function sayHelloMisc(){
  
  alert("misc.js is linked");
}

var Misc = {

  _100_PCTG           : 100,
  _CHECK_TRUE         : "true",
  _CHECK_FALSE        : "false",
  _TYPE_IN            : "IN",
  _TYPE_OUT           : "OUT",
  _UNKNOWN            : "?",
  _SUPPLIER           : "Supplier",
  _CUSTOMER           : "Customer",
  _COMMA_SEPARATOR    : ",",
  _CHECK_TRUE         : "true",
  _CHECK_FALSE        : "false",
  _CASH_BANK_IN       : "IN",
  _CASH_BANK_OUT      : "OUT",
  _CASH_BANK_INVOICE  : "Invoice",
  _CASH_BANK_DP       : "DP",
  _CASH_BANK_CCL_DP   : "DP Cancel",

  computeAll : function (totInvoice,
                         totPayment,
                         gainEnt,
                         discEnt){
                         
    totInvoice  = document.getElementsByName(totInvoice);
    totPayment  = document.getElementsByName(totPayment);
    gainEnt     = document.getElementsByName(gainEnt);
    discEnt     = document.getElementsByName(discEnt);
    
    totInvoice  = GMisc.firstElement(totInvoice);
    totPayment  = GMisc.firstElement(totPayment);
    gainEnt     = GMisc.firstElement(gainEnt);
    discEnt     = GMisc.firstElement(discEnt);
    
    var tmp = parseFloat(totPayment.value) -
              parseFloat(totInvoice.value) -
              parseFloat(gainEnt.value);
              
    discEnt.value = tmp.toFixed(2);
  },

  setInvoice : function (thisObj,
                         prevStatus,
                         srcInvoice){
  
    srcInvoice = document.getElementsByName(srcInvoice);
    prevStatus = document.getElementsByName(prevStatus);
    
    srcInvoice = GMisc.firstElement(srcInvoice);
    prevStatus = GMisc.firstElement(prevStatus);
    
    if(thisObj.checked == true){
    
      thisObj.value    = srcInvoice.value;
    
      prevStatus.value = this._CHECK_TRUE;
    }else{
    
      /**
      * Checked if previous checked is true
      */
      if(prevStatus.value == this._CHECK_TRUE){
      
        thisObj.value    = "";
                       
        prevStatus.value = this._CHECK_FALSE;               
      }else{
      
        /*Do nothing*/
      }
    }
    
  },

  txWindow : function (srcHostIp,
                       whoseName,
                       whoseCode,
                       currency,
                       rate,
                       totSrcNet,
                       isInOrOut,
                       tx){

    whoseName  = document.getElementsByName(whoseName);
    whoseCode  = document.getElementsByName(whoseCode);
    currency   = document.getElementsByName(currency);
    rate       = document.getElementsByName(rate);
    totSrcNet  = document.getElementsByName(totSrcNet);
    isInOrOut  = document.getElementsByName(isInOrOut);
    tx         = document.getElementsByName(tx);
    
    whoseName  = GMisc.firstElement(whoseName);
    whoseCode  = GMisc.firstElement(whoseCode);
    currency   = GMisc.firstElement(currency);
    rate       = GMisc.firstElement(rate);
    totSrcNet  = GMisc.firstElement(totSrcNet);
    isInOrOut  = GMisc.firstElement(isInOrOut);
    tx         = GMisc.firstElement(tx);

    switch(isInOrOut.value){
    
      case this._CASH_BANK_IN:
      
        switch(tx.value){
        
          case this._CASH_BANK_INVOICE:
          
            var targetFile = "inputInvoice_html.php";
            break;
          
          case this._CASH_BANK_DP:
          
            var targetFile = "inputDP_html.php";
            break;
          
          case this._CASH_BANK_CCL_DP:
          
            var targetFile = "";
            break;
            
          default:
            
            /**
             * Immediately exit()
             */
            return false;
        }
        break;
        
      case this._CASH_BANK_OUT:
      
        switch(tx.value){
        
          case this._CASH_BANK_INVOICE:
          
            var targetFile = "outputInvoice_html.php";
            break;
          
          case this._CASH_BANK_DP:
          
            var targetFile = "outputDP_html.php";
            break;
           
          case this._CASH_BANK_CCL_DP:
          
            var targetFile = "";
            break;
            
          default:
            
            /**
             * Immediately exit()
             */
            return false;
        }
        break;
        
      default:
        
        /**
         * Immediately exit()
         */
        return false;
    }
    
    var _BASE_HTML = "http://"                + srcHostIp  + 
                     "/ecs/tocmenu_43/views/" + targetFile ;
    
    /**
    * Fetch the opener properties
    *
    */
    var _HEIGHT = 600 + this._COMMA_SEPARATOR;
    var _WIDTH  = 800 + this._COMMA_SEPARATOR;
    
    hdlChild = window.open((_BASE_HTML       +
                            "?whoseName="    + whoseName.value +
                            "&whoseCode="    + whoseCode.value +
                            "&currency="     + currency.value  +
                            "&rate="         + rate.value      +
                            "&totSrcNet="    + totSrcNet.value),
                           "Invoice List",
                           ("height=" + _HEIGHT + 
                            "width="  + _WIDTH  + 
                            "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizeable=1"));
  
  },

  procDrop : function (thisObj,
                       prevStatus,
                       hrzTotEnt,
                       totHrzTotEnt,
                       discEnt,
                       totDiscEnt,
                       netEnt,
                       totNetEnt){
  
    prevStatus    = document.getElementsByName(prevStatus);
    
    prevStatus    = GMisc.firstElement(prevStatus);
    
    
    if(thisObj.checked == true){
    
      GMisc.syncVal2F(hrzTotEnt,
                      totHrzTotEnt);
      
      GMisc.syncVal2F(discEnt,
                      totDiscEnt);
      
      GMisc.syncVal2F(netEnt,
                      totNetEnt);
    
      prevStatus.value = this._CHECK_TRUE;
    }else{
    
      /**
      * Checked if previous checked is true
      */
      if(prevStatus.value == this._CHECK_TRUE){
      
        GMisc.sumVal2F(hrzTotEnt,
                       totHrzTotEnt);
	      
	      GMisc.sumVal2F(discEnt,
	                     totDiscEnt);
	      
	      GMisc.sumVal2F(netEnt,
	                     totNetEnt);
	                     
	      prevStatus.value = this._CHECK_FALSE;               
      }else{
      
        /*Do nothing*/
      }
    }
  },

  miniView : function (srcHostIp,
	                     srcModel, 
	                     srcNota){
  
	  var _SEPARATOR_2ND    = "~^~";
	  var _1ST              = 0;
	  var _2ND              = 1;
	  var _COMMA_SEPARATOR  = ",";
	  var _SMALLER          = 200;
	  var _BASE_HTML        = "http://" + srcHostIp + "/ecs/tocmenu_43/views/miniView_html.php";
	  var _TYPE_PO          = 1;
	  
	  /**
	  * Fetch the opener properties
	  *
	  */
	  var _HEIGHT = 250 + _COMMA_SEPARATOR;
	  var _WIDTH  = 220 + _COMMA_SEPARATOR;
	  
	  srcModel   = document.getElementsByName(srcModel);
    srcNota    = document.getElementsByName(srcNota);
	  
	  srcModel   = GMisc.firstElement(srcModel);
    srcNota    = GMisc.firstElement(srcNota);
	  
	  hdlChild = window.open((_BASE_HTML + 
	                          "?srcModel="    + srcModel.value +
	                          "&srcNota="     + srcNota.value  ),
	                         
	                         "miniView",
	                         ("height=" + _HEIGHT + 
	                          "width="  + _WIDTH  + 
	                          "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizeable=0"));
	
	},

  hrzTot : function (srcQty,
                     srcPrice,
                     thisObj){
  
    srcQty   = document.getElementsByName(srcQty);
    srcPrice = document.getElementsByName(srcPrice);
    
    srcQty    = GMisc.firstElement(srcQty);
    srcPrice  = GMisc.firstElement(srcPrice);
    
    thisObj.value = parseFloat(srcQty.value) * parseFloat(srcPrice.value);
  },

  setPayable : function(amount,
                        tax,
                        expense,
                        prepaid,
                        payable){
  
    amount   = document.getElementsByName(amount);
    tax      = document.getElementsByName(tax);
    expense  = document.getElementsByName(expense);
    prepaid  = document.getElementsByName(prepaid);
    payable  = document.getElementsByName(payable);
    
    amount    = GMisc.firstElement(amount);
    tax       = GMisc.firstElement(tax);
    expense   = GMisc.firstElement(expense);
    prepaid   = GMisc.firstElement(prepaid);
    payable   = GMisc.firstElement(payable);
    
    var tmp = parseFloat(amount.value)  - 
              parseFloat(tax.value)     - 
              parseFloat(expense.value) - 
              parseFloat(prepaid.value);
              
    payable.value = tmp.toFixed(2);
  },

  setXPctg : function(srcObject,
	                    thisObject,
	                    dstObject){
  
    srcObject   = document.getElementsByName(srcObject);
	  thisObject  = document.getElementsByName(thisObject);
	  dstObject   = document.getElementsByName(dstObject);
	  
	  srcObject   = GMisc.firstElement(srcObject);
	  thisObject  = GMisc.firstElement(thisObject);
	  dstObject   = GMisc.firstElement(dstObject);
	  
	  /**
	  * calc the amount of discount
	  * result = srcAmount * (dscPctg / 100Pctg)
	  *
	  */
	  var tmpResult = parseFloat(srcObject.value)   * 
	                  (parseFloat(thisObject.value) / this._100_PCTG);
	                  
	  dstObject.value = tmpResult.toFixed(2);
  }
}
